<template>
    <el-row :gutter="20" id="backaccount" v-cloak>
        <el-col :span="14">
            <div class="block mt">
                <el-select class="w200" size="small" @change="selGuest" v-model="ginfo.name" filterable remote reserve-keyword placeholder="客户姓名 / 电话 / 卡号" :remote-method="queryGuest" :loading="loading">
                    <el-option v-for="(g, k) in glist" :label="g.name" :value="Object.assign({}, g)">
                        {{g.name}} -- {{g.vipcard}}
                    </el-option>
                </el-select>
                <el-tag class="ml" v-show="ginfo.id">{{ginfo.sex?'男':'女'}}</el-tag>
                <el-tag class="ml" v-show="ginfo.id">电话：{{ginfo.phone}}</el-tag>
                <el-tag class="ml" v-show="ginfo.id">卡号：{{ginfo.vipcard}}</el-tag>
                <div class="block mt" v-show="ginfo.id">
                    <el-tag>账户余额：{{ginfo.balance}} 元</el-tag>
                    <el-tag class="ml">赠送余额：{{ginfo.giftbalance}} 元</el-tag>
                </div>
            </div>
            <el-input v-model="backmoney" placeholder="请输入退款金额！" class="mt mb">
                <template slot="prepend">退款金额</template>
            </el-input>
            <el-input type="textarea" placeholder="请输入退款说明" v-model="remarks"></el-input>
            <!-- 确定·提交 -->
            <div class="block mt">
                <el-button type="success" icon="el-icon-upload" @click="submit">确定退款</el-button>
                <el-button type="danger" icon="el-icon-close" @click="close">取消关闭</el-button>
            </div>
        </el-col>
        <el-col :span="10">
            <el-table :data="recharges" :stripe="true" class="mt" height="530">
                <el-table-column prop="type" label="类型" width="60">
                    <template slot-scope="scope">
                        <el-tag size="mini" v-if="scope.row.type===1">充值</el-tag>
                        <el-tag size="mini" type="danger" v-else-if="scope.row.type===2">欠款</el-tag>
                        <el-tag size="mini" type="success" v-else-if="scope.row.type===3">消费</el-tag>
                        <el-tag size="mini" type="warning" v-else-if="scope.row.type===4">退款</el-tag>
                        <el-tag size="mini" type="info" v-else>其他</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="balance" label="余额信息"></el-table-column>
                <el-table-column prop="giftbalance" label="赠送信息"></el-table-column>
                <el-table-column prop="addtime" label="时间" :show-overflow-tooltip="true">
                    <template slot-scope="scope">{{time(scope.row.addtime)}}</template>
                </el-table-column>
            </el-table>
            <div class="block center bg-light">
                <el-pagination @current-change="currentChange" :page-size="page.limits" :current-page="page.pages" layout="total, prev, pager, next" :total="total">
                </el-pagination>
            </div>
        </el-col>
    </el-row>
</template>
<script>
import C from '#/src/common/common.js'

export default {
    name: 'backAccount',
    data() {
        return {
            ginfo: {},
            backmoney: '', // 需要退还的金额
            remarks: '', // 退款说明
            recharges: [], // 余额使用情况
            page: {
                limits: 10,
                pages: 1,
                gid: 0,
            },
            total: 0,
            glist: [],
            timeout: null,
            loading: false
        }
    },
    methods: {
        queryGuest: function(query) {
            if (query !== '') {
                this.loading = true
                clearTimeout(this.timeout)
                this.timeout = setTimeout(async() => {
                    let res = await C.ajax('/shop/backcard/queryguests', { query: query })
                    if (res) {
                        this.loading = false
                        this.glist = res
                    }
                }, 1000)
            } else {
                this.glist = []
            }
        },

        selGuest: function(row) {
            this.ginfo = row
            this.page.gid = row.id
            this.queryList()
        },

        currentChange: function(val) {
            this.page.pages = val
            this.queryList()
        },

        queryList: async function() {
            let res = await C.ajax('/shop/backcard/getrechargeinfo', this.page, 'post')
            if (res) {
                this.recharges = res.data
                this.total = res.count
            }
        },

        time: function(unix) {
            return C.time(unix)
        },
        close() {
            C.closewin()
        },

        submit: async function() {
            if (!this.ginfo.id) {
                C.error('请选择需要退款的客户')
                return false
            } else if (!/^[1-9]\d*$/.test(this.backmoney)) {
                C.error('请输入退款金额')
                return false
            } else if (this.backmoney * 1 > this.ginfo.balance) {
                C.error('退款金额不能超过顾客账户余额')
                return false
            }

            let data = {
                gid: this.ginfo.id,
                backmoney: this.backmoney,
                remarks: this.remarks
            }
            C.confirm('确定退款？', async() => {
                let res = await C.ajax('/shop/backcard/backaccount', data, 'post')
                if (res) {
                    C.closeBrowserWindow(res)
                }
            })
        }
    }
}
</script>
<style>
.ov {
    overflow: hidden;
}

.ov-x {
    overflow-x: hidden;
}

.fl {
    float: left;
}

.fr {
    float: right;
}

.clear {
    clear: both;
}

.none {
    display: none;
}

.block {
    display: block;
}

.handel {
    cursor: pointer;
}







/*背景颜色-前景颜色*/

.bg-dark {
    background: #F9FAFC;
}

.bg-extra {
    background: #EFF2F7;
}

.bg-light {
    background: #E5E9F2;
}

.bg-gray {
    background: #D3DCE6;
}

.bg-silver {
    background: #8492A6;
}

.bg-black {
    background: #324057;
}

.white {
    color: white;
}

.black {
    color: #324057;
}

.green {
    color: green;
}

.blue {
    color: blue;
}

.red {
    color: red;
}

.color-nan {
    color: #2a91f7;
}

.color-nv {
    color: #f7992a;
}







/*其他样式*/

html {
    font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif;
    font-weight: 400;
}

.title {
    font-size: 16px;
    font-family: "Helvetica Neue", "Helvetica", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
    line-height: 26px;
    margin: 10px 0;
}

.border {
    border: 1px solid #dfe6ec;
    border-radius: 4px;
    padding: 0 8px;
}

.bd {
    border: 1px solid #dfe6ec;
}

.border-b {
    border-bottom: 1px solid #dfe6ec;
}

.border-t {
    border-top: 1px solid #dfe6ec;
}

.bd-l {
    border-left: 1px dashed #dfe6ec;
}

.bd-t {
    border-top: 1px dashed #dfe6ec;
}

.bd-r {
    border-radius: 50%;
}

.underline {
    text-decoration: underline;
}







/*边距*/

.p0 {
    padding: 0 !important;
}

.m0 {
    margin: 0 !important;
}

.m10 {
    margin: 10px !important;
}

.m15 {
    margin: 15px !important;
}

.p10 {
    padding: 10px !important;
}

.p15 {
    padding: 15px !important;
}

.ml {
    margin-left: 15px !important;
}

.mr {
    margin-right: 15px !important;
}

.mb {
    margin-bottom: 15px !important;
}

.mt {
    margin-top: 15px !important;
}

.pl {
    padding-left: 15px !important;
}

.pl0 {
    padding-left: 0 !important;
}

.pr {
    padding-right: 15px !important;
}

.pr0 {
    padding-right: 0 !important;
}

.pb {
    padding-bottom: 15px !important;
}

.pt {
    padding-top: 15px !important;
}

.center {
    text-align: center !important;
}

.left {
    text-align: left !important;
}

.right {
    text-align: right !important;
}

.w100 {
    width: 100px !important;
}

.w150 {
    width: 150px !important;
}

.w200 {
    width: 200px !important;
}

.w300 {
    width: 300px !important;
}

.w400 {
    width: 400px !important;
}

.w500 {
    width: 500px !important;
}

.w30 {
    width: 30%;
}

.w50 {
    width: 50%;
}

.width95 {
    width: 95%;
}

.width {
    width: 100% !important;
}

.h200 {
    height: 200px;
}

.h300 {
    height: 300px;
}

.h500 {
    height: 500px;
}

.h600 {
    height: 600px;
}

.h700 {
    height: 700px;
}

.h800 {
    height: 800px;
}

.l28 {
    line-height: 28px;
}

.l30 {
    line-height: 30px;
}

.l50 {
    line-height: 50px;
}







/*附加订制样式*/

.el-row {
    margin: 0 !important;
}

.el-table .cell {
    padding: 0;
    text-align: center;
}

.el-textarea__inner {
    resize: none;
}

.el-input.is-disabled .el-input__inner {
    color: #20A0FF;
}

.el-textarea.is-disabled .el-textarea__inner {
    color: #444;
}

.el-switch {
    line-height: 0;
}

.el-checkbox+.el-checkbox {
    margin-left: 0;
}

.el-checkbox {
    margin-right: 20px;
}

.el-autocomplete-suggestion li {
    padding: 4px 20px;
    line-height: 20px;
}

.el-table th {
    padding: 10px 0;
}

.el-table td {
    padding: 4px 0;
}
</style>